package coco.usbhid

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Point
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.WindowManager
import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity
import coco.usbhid.UsbManage.keyReport
import coco.usbhid.UsbManage.mouseReport
import coco.usbhid.UsbManage.sendData
import coco.usbhid.UsbManage.touchReport
import com.yanzhenjie.andserver.AndServer
import com.yanzhenjie.andserver.Server
import java.lang.Thread.sleep
import java.util.concurr     nt.TimeUnit



fun getScreenHeight(): Int {
    return Resources.getSystem().displayMetrics.heightPixels
}

fun getScreenWidth(): Int {
    return Resources.getSystem().displayMetrics.widthPixels
}
fun getWifiIP(context: Context ): String {
    val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
    @SuppressLint("MissingPermission") val wifiInfo =
        wifiManager.connectionInfo
    val ipAddress = wifiInfo.getIpAddress()
    return String.format(
        "%d.%d.%d.%d",
        ipAddress and 0xff,
        ipAddress shr 8 and 0xff,
        ipAddress shr 16 and 0xff,
        ipAddress shr 24 and 0xff
    )
}
fun startServer(context:Context) {
    val server = context.let {
        AndServer.webServer(it)
            .port(8085)
            .timeout(10, TimeUnit.SECONDS)
            .listener(object : Server.ServerListener {
                override fun onStarted() {
//                        Aip="http://${getWifiIP()}:8080"
                    val handler = Handler(Looper.getMainLooper())
                    handler.post {
                        Toast.makeText(context,  "服务器绑定地址:" + "http://${getWifiIP(context)}:8085", Toast.LENGTH_SHORT).show()
                    }

                    println(
                        "服务器绑定地址:" + "http://${getWifiIP(context)}:8085"
                    )
                }
                override fun onStopped() {
                }

                override fun onException(e: Exception) {
                    e.printStackTrace()
                }
            })
            .build()
    }
    server?.startup()
}
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        UsbManage.init(this)
        ScreenManager.init(this);
        startServer(this)

        Handler(Looper.getMainLooper()).postDelayed({
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(Intent(this, ForegroundService::class.java))
            } else {
                startService(Intent(this, ForegroundService::class.java))
            }
        }, 1000) // 1秒延迟
//        keyReport(0, listOf(4))
//
//        keyReport(0, listOf(0))
//        Thread {
//            sleep(5000)
//            println("开始点击")
//            sendData(touchReport(3, 100,200))
//            sleep(1000)
//            println("开始释放")
//            sendData(touchReport(3, 20,30))
//        }.start()

    }
}